﻿using System.Text.RegularExpressions;

namespace IFD2.Helpers
{
    public class UserAgent
    {
        public UserAgent()
        {
        }

        public static string Parse(string user_agent,bool return_ori)
        {
            string os, browser, os_ver,browser_ver;
            MatchCollection results,results2;
            string[] temp;

            //http://useragentstring.com/pages/useragentstring.php
            /***************************************************/
        //parsePlatform:
            results = Regex.Matches(user_agent, @"Android \d{1,6}\.\d{1,6}");
            if(results.Count>=1)
            {
                temp = results[0].Value.Split(' ');
                os = temp[0];
                os_ver = temp[1];
                goto parse_browser;
            }

            results = Regex.Matches(user_agent, @"Linux");
            if (results.Count >= 1)
            {
                os = "Linux";
                os_ver = "?";
                goto parse_browser;
            }

            //Mozilla/5.0 (BlackBerry; U; BlackBerry 9780; ja) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.587 Mobile Safari/534.8+
            results = Regex.Matches(user_agent, @"BlackBerry");
            if (results.Count >= 1)
            {
                os = "BlackBerry";
                os_ver = "?";
                goto parse_browser;
            }

            //Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; Xbox)
            results = Regex.Matches(user_agent, @"Xbox");
            if (results.Count >= 1)
            {
                os = "Xbox";
                os_ver = "?";
                goto parse_browser;
            }

            //Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
            results = Regex.Matches(user_agent, @"Windows NT \d{1,6}\.\d{1,6}");
            if (results.Count >= 1)
            {
                temp = results[0].Value.Split(' ');
                os = "Windows";
                os_ver = temp[2];
                if (os_ver == "6.1")
                {
                    os_ver = "7/Server 2008 R2";
                }
                else if (os_ver == "10.0")
                {
                    os_ver = "10";
                }
                else if (os_ver == "5.0")
                {
                    os_ver = "2000";
                }
                else if (os_ver == "5.1")
                {
                    os_ver = "XP";
                }
                else if (os_ver == "5.01")
                {
                    os_ver = "2000";
                }
                else if (os_ver == "4.0")
                {
                    os_ver = "NT 4.0";
                }
                else if (os_ver == "5.2")
                {
                    os_ver = "Server 2003/XP x64";
                }
                else if (os_ver == "6.0")
                {
                    os_ver = "Vista/Server 2008";
                }
                else if (os_ver == "6.2")
                {
                    os_ver = "8/Server 2012";
                }
                else if (os_ver == "6.3")
                {
                    os_ver = "8.1/Server 2012 R2";
                }
                goto parse_browser;
            }

            results = Regex.Matches(user_agent, @"Windows CE");
            if (results.Count >= 1)
            {
                temp = results[0].Value.Split(' ');
                os = "Windows";
                os_ver = "CE";
                goto parse_browser;
            }
            results = Regex.Matches(user_agent, @"Win 9x");
            if (results.Count >= 1)
            {
                temp = results[0].Value.Split(' ');
                os = "Windows";
                os_ver = "ME";
                goto parse_browser;
            }
            //Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
            results = Regex.Matches(user_agent, @"Windows NT");
            if (results.Count >= 1)
            {
                temp = results[0].Value.Split(' ');
                os = "Windows";
                os_ver = "NT";
                goto parse_browser;
            }

            //Mozilla / 4.0(compatible; MSIE 5.0; Windows 98; DigExt)
            results = Regex.Matches(user_agent, @"Windows \d{1,6}");
            if (results.Count >= 1)
            {
                temp = results[0].Value.Split(' ');
                os = "Windows";
                os_ver = temp[1];
                goto parse_browser;
            }
            
            //Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)
            results = Regex.Matches(user_agent, @"Windows Phone OS \d{1,6}\.\d{1,6}");
            if (results.Count >= 1)
            {
                temp = results[0].Value.Split(' ');
                os = "WinPhone";
                os_ver = temp[3];
                goto parse_browser;
            }

            //Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; DEVICE INFO) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Mobile Safari/537.36 Edge/12.<OS build number>
            results = Regex.Matches(user_agent, @"Windows Phone \d{1,6}\.\d{1,6}");
            if (results.Count >= 1)
            {
                temp = results[0].Value.Split(' ');
                os = "WinPhone";
                os_ver = temp[1];
                goto parse_browser;
            }

            //HTC_Touch_3G Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 7.11)
            results = Regex.Matches(user_agent, @"Windows CE");
            if (results.Count >= 1)
            {
                temp = results[0].Value.Split(' ');
                os = "Windows";
                os_ver = "CE";
                goto parse_browser;
            }

            //Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_1 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5
            results = Regex.Matches(user_agent, @"iPhone;");
            if (results.Count >= 1)
            {
                os = "iPhone";
                os_ver = "?";
                goto parse_browser;
            }

            //Mozilla/5.0 (iPod; U; CPU iPhone OS 4_1 like Mac OS X; ja-jp) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B118 Safari/6531.22.7
            results = Regex.Matches(user_agent, @"iPod;");
            if (results.Count >= 1)
            {
                os = "iPod";
                os_ver = "?";
                goto parse_browser;
            }

            //Mozilla/5.0 (iPad; U; CPU OS 4_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F190 Safari/6533.18.5
            results = Regex.Matches(user_agent, @"iPad;");
            if (results.Count >= 1)
            {
                os = "iPad";
                os_ver = "?";
                goto parse_browser;
            }

            //Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.22 (KHTML, like Gecko) Chrome/19.0.1047.0 Safari/535.22
            results = Regex.Matches(user_agent, @"Macintosh;");
            if (results.Count >= 1)
            {
                os = "Macintosh";
                os_ver = "?";
                goto parse_browser;
            }

            //else
            if (return_ori)
                return (user_agent);
            else
                return ("?");

            /***************************************************/
        parse_browser:

            results = Regex.Matches(user_agent, @"Maxthon");
            if (results.Count >= 1)
            {
                browser = "Maxthon";
                browser_ver = "?";
                goto parse_end;
            }

            results = Regex.Matches(user_agent, @"MQQBrowser");
            if (results.Count >= 1)
            {
                browser = "MQQBrowser";
                browser_ver = "?";
                goto parse_end;
            }
            results = Regex.Matches(user_agent, @"QQBrowser");
            if (results.Count >= 1)
            {
                browser = "QQBrowser";
                browser_ver = "?";
                goto parse_end;
            }
            results = Regex.Matches(user_agent, @"UCWEB");
            if (results.Count >= 1)
            {
                browser = "UC";
                browser_ver = "?";
                goto parse_end;
            }
            results = Regex.Matches(user_agent, @"TaoBrowser");
            if (results.Count >= 1)
            {
                browser = "TaoBao";
                browser_ver = "?";
                goto parse_end;
            }
            results = Regex.Matches(user_agent, @"LBBROWSER");
            if (results.Count >= 1)
            {
                browser = "LieBao";
                browser_ver = "?";
                goto parse_end;
            }
            results = Regex.Matches(user_agent, @"360SE");
            if (results.Count >= 1)
            {
                browser = "360SE";
                browser_ver = "?";
                goto parse_end;
            }
            results = Regex.Matches(user_agent, @"360EE");
            if (results.Count >= 1)
            {
                browser = "360EE";
                browser_ver = "?";
                goto parse_end;
            }
            results = Regex.Matches(user_agent, @"baidubrowser");
            if (results.Count >= 1)
            {
                browser = "Baidu";
                browser_ver = "?";
                goto parse_end;
            }
            results = Regex.Matches(user_agent, @"MetaSr");
            if (results.Count >= 1)
            {
                browser = "Sougou";
                browser_ver = "?";
                goto parse_end;
            }
            results = Regex.Matches(user_agent, @"TheWorld");
            if (results.Count >= 1)
            {
                browser = "TheWorld";
                browser_ver = "?";
                goto parse_end;
            }
            //results = Regex.Matches(user_agent, @"TencentTraveler");//过时
            //if (results.Count >= 1)
            //{
            //    browser = "TT";
            //    browser_ver = "?";
            //    goto parse_end;
            //}

            //Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.<OS build number>
            results = Regex.Matches(user_agent, @"Edge/\d{1,6}");
            if (results.Count >= 1)
            {
                temp = results[0].Value.Split('/');
                browser = "Edge";
                browser_ver = temp[1];
                goto parse_end;
            }

            //Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
            //一定要在safari之前，因为全包含Safari
            results = Regex.Matches(user_agent, @"Chrome/\d{1,6}");
            if (results.Count >= 1)
            {
                temp = results[0].Value.Split('/');
                browser = temp[0];
                browser_ver = temp[1];
                goto parse_end;
            }

            //Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2
            //Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1 Mobile Safari/534.57.2
            results = Regex.Matches(user_agent, @"Safari/\d{1,6}");
            results2 = Regex.Matches(user_agent, @"Version/\d{1,6}");
            if (results.Count >= 1 && results2.Count >=1)
            {
                temp = results2[0].Value.Split('/');
                browser = "Safari";
                browser_ver = temp[1];
                goto parse_end;
            }
            else if(results.Count>=1)//Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3
            {
                temp = results[0].Value.Split('/');
                browser = "Safari";
                browser_ver = temp[1];
                goto parse_end;
            }

            //Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; ja) Opera 8.01
            //Opera/9.00 (Macintosh; PPC Mac OS X; U; ja)
            //Mozilla/4.0 (compatible; Windows Mobile; WCE; Opera Mobi/WMD-50433; U; de) Presto/2.4.13 Version/10.00 异类未处理！
            //一定要在MSIE之前，因为包含MSIE
            results = Regex.Matches(user_agent, @"Opera( |/)\d{1,6}");
            if (results.Count >= 1)
            {
                browser = results[0].Value;
                results = Regex.Matches(browser, @"\d{1,6}");
                browser = "Opera";
                browser_ver = results[0].Value;
                goto parse_end;
            }

            //Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
            results = Regex.Matches(user_agent, @"MSIE \d{1,6}");
            if (results.Count >= 1)
            {
                temp = results[0].Value.Split(' ');
                browser = "IE";
                browser_ver = temp[1];
                goto parse_end;
            }

            //Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
            results = Regex.Matches(user_agent, @"Trident/\d{1,6}");
            results2 = Regex.Matches(user_agent, @" rv:\d{1,6}");
            if (results.Count >= 1 && results2.Count >= 1)
            {
                temp = results2[0].Value.Split(':');
                browser = "IE";
                browser_ver = temp[1];
                goto parse_end;
            }

            //Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6
            results = Regex.Matches(user_agent, @"Firefox/\d{1,6}");
            if (results.Count >= 1)
            {
                temp = results[0].Value.Split('/');
                browser = "Firefox";
                browser_ver = temp[1];
                goto parse_end;
            }

            //else
            if (return_ori)
                return (user_agent);
            else
                return ("?");

            /**********************************************************/
            parse_end:
            if (os_ver != "?")
                os = os + ":" + os_ver;
            if (browser_ver != "?")
                browser = browser + ":" + browser_ver;

            return os + "; " + browser;
        }
    }
}